﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmoneyWeiXinEntity;
using EmoneyWeiXinCommon;
using System.Data.SqlClient;
using System.Data;
namespace EmoneyWeiXinDAL
{
    public class UserGroupManagerDal
    {
        public static List<UserGroupManagerEntity> getUserGroupManagerList(int pindex, int psize, int weixinid, out int total)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@pindex",pindex),
            new SqlParameter("@psize", psize),
            new SqlParameter("@total",0),
            new SqlParameter("@weixinid",weixinid)
            };
            parameters[2].Direction = ParameterDirection.Output;
            List<UserGroupManagerEntity> result = SQLHelper.GetProcedureDataByColumnsLst<UserGroupManagerEntity>(Config.EmoneyWeiXin, "sp_UserGroupManager_Select", parameters);
            total = Convert.ToInt32(parameters[2].Value);
            return result;
        }

        public static List<UserGroupManagerEntity> getUserGroupManagerList(int weixinid)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@weixinid",weixinid)
            };
            List<UserGroupManagerEntity> result = SQLHelper.GetProcedureDataByColumnsLst<UserGroupManagerEntity>(Config.EmoneyWeiXin, "sp_UserGroupManager_SelectByWeixinID", parameters);
            return result;
        }

        public static List<UserGroupManagerEntity> getUserGroupManagerListByID(int id)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@id",id)
            };
            List<UserGroupManagerEntity> result = SQLHelper.GetProcedureDataByColumnsLst<UserGroupManagerEntity>(Config.EmoneyWeiXin, "sp_UserGroupManager_SelectByID", parameters);
            return result;
        }

        public static int UpdateUserGroupManager(UserGroupManagerEntity obj)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@id" , obj.id),
            new SqlParameter("@name", obj.name),
            new SqlParameter("@createtime", obj.createtime),
            new SqlParameter("@isvalid", obj.isvalid),
            new SqlParameter("@creator", obj.creator),
            new SqlParameter("@updatetime", obj.updatetime),
            new SqlParameter("@weixinid" , obj.weixinid),
            new SqlParameter("@wx_groupid" , obj.wx_groupid)
            };
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_UserGroupManager_Update", parameters);
            return result;
        }
        public static int DeleteUserGroupManager(int id)
        {
            SqlParameter[] parameters = new SqlParameter[] {  
            new SqlParameter("@id", id)};
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_UserGroupManager_Delete", parameters);
            return result;
        }

        public static DataSet GetProvinceList()
        {
            DataSet ds = SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_Povince_Select", null);
            return ds;
        }
        public static DataSet GetCityListByPname(string pname)
        {
            SqlParameter[] parameters = new SqlParameter[] {  
            new SqlParameter("@pname", pname)};
            DataSet ds = SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_City_SelectByPname", parameters);
            return ds;
        }

        public static DataSet GetOpenidListByGroupId(string weixinId, string groupId)
        {
            string sql = "SELECT openId from  UserGroupRel where weixinId=@weixinId AND groupId=@groupId";
            string sql2 = "  select wx_groupid from UserGroupManager where id=@groupId";
            SqlParameter[] paras = 
            {
              new SqlParameter("@weixinId",weixinId),
              new SqlParameter("@groupId",groupId)
            };
            DataSet dt = SQLHelper.ExecuteGetDataSet(Config.EmoneyWeiXin, CommandType.Text, sql + sql2, paras);
            return dt;
        }

        public static UserGroupManagerEntity GetOne(string groupId)
        {
            string sql = "SELECT * FROM UserGroupManager WHERE id=@groupId";
            SqlParameter[] paras = 
            {
              new SqlParameter("@groupId",groupId)
            };
            List<UserGroupManagerEntity> lst = SQLHelper.GetDataByColumnsLst<UserGroupManagerEntity>(Config.EmoneyWeiXin, sql, paras);
            if (lst.Count > 0)
            {
                return lst[0];
            }
            else
            {
                return null;
            }
        }


    }
}
